<?php
/*----------------------------------------------------------------------
 * 日志管理
 * Author: ming.nie
----------------------------------------------------------------------*/

namespace Common\Logic;

class LogLogic{
	
	/**
	 * 获取日志列表
	 * @param  array
	 * @return array
	 * @author ming.nie
	 */
	public function getLogList($params)
	{
		$result = array();
		
        $model = M();
        //查询语句
        $select_sql = " AND a.is_show=1";
        $order_sql = "";
        //组织查询条件sql
		if(array_key_exists('keywords', $params) && $params['keywords'])
        {
            $select_sql .= " AND (b.user_name LIKE '%%".$params['keywords']."%%' OR b.nick_name LIKE '%%".$params['keywords']."%%')";
        }
		if($params['keywords'] == '0'){
			$select_sql .= " AND (b.user_name LIKE '%%".$params['keywords']."%%' OR b.nick_name LIKE '%%".$params['keywords']."%%')";
		}
		if(isset($params['option']) && $params['option'])
        {
            $select_sql .= " AND (a.action = '".$params['option']."')";
        }
		if(isset($params['create_time_start']) && !empty($params['create_time_start']))
        {
            $select_sql .= " AND (a.create_time >= ".strtotime($params['create_time_start']).")";
        }
        if(isset($params['create_time_end']) && !empty($params['create_time_end']))
        {
            $create_time_end = strtotime($params['create_time_end']);
            $create_time_end += 86400;
            $select_sql .= " AND (a.create_time <= ".$create_time_end.")";
        }
		
        //查询语句
        $sql = "SELECT %s
                FROM mt_user_log AS a
				LEFT JOIN mt_user AS b ON a.create_user_id = b.user_id 
                WHERE 1".$select_sql." ORDER BY a.log_id DESC";
        //统计个数，准备分页
        $field_aql = "COUNT(*) AS count";
        $current_sql = sprintf($sql,$field_aql);
        $rs_count = $model->query($current_sql);
        $count = $rs_count[0]['count'];
        if(isset($params['page']))
        {
            $_GET['p'] = $params['page'];
        }
		$pager = new \Think\Page2($count, $params['page_size'], $_REQUEST);
		
        //查询数据
        $field_aql = "a.*,b.user_name,b.nick_name";
        $current_sql = sprintf($sql,$field_aql);
        $current_sql .= " LIMIT ".$pager->firstRow.",".$pager->listRows;
        $data_list = array();
        $data_list = $model->query($current_sql);
		//echo $model->getLastSql();
        if(!empty($data_list))
        {
            foreach($data_list as $key => $value)
            {
				$temp = $value;
				$data_list[$key] = $temp;
            }
        }
        
        $result['count'] = $count;
        $result['data_list'] = $data_list;
        $result['pager'] = $pager->show();
        
        return $result;
	}

	/**
	 * 添加用户日志
	 * @param  array
	 * @return array
	 * @author ming.nie
	 */
	public function add($params)
	{
		$model = M('UserLog');
        $params['module'] = MODULE_NAME;
        $params['controller'] = CONTROLLER_NAME;
        $params['method'] = ACTION_NAME;
        $params['action'] = __ACTION__;
        $params['get_data'] = json_encode($_GET);
        $params['post_data'] = json_encode($_POST);
        $params['ip'] = get_client_ip();
		$params['create_time'] = time();
		$params['content'] = '';
		
		//判断是否属于统计范围
		$log_options = getLogOptions();
		if(array_key_exists($params['action'], $log_options)) {
			$params['is_show'] = 1;
			//基于数据，写入具体内容
			if(U('/Admin/Login/index') == $params['action']) {
				//登录用户
				$params['content'] = $_POST['user_name'];
			}
			if(U('/Admin/User/add') == $params['action']) {
				//添加用户
				$params['content'] = $_POST['user_name'];
			}
			if(U('/Admin/User/edit') == $params['action']) {
				//编辑用户
				$params['content'] = $_POST['user_name'];
			}
			if(U('/Admin/User/update') == $params['action']) {
				//禁用用户
				if(array_key_exists('status', $_REQUEST))
				$status = I('request.status');
				$user_name = I('request.user_name');
				if(1 == $status) {
					$params['is_show'] = 0;
				}
				if(2 == $status && $user_name) {
					$params['is_show'] = 1;
					$params['content'] = $user_name;
				}
			}
			if(U('/Admin/User/addGroup') == $params['action']) {
				//添加角色
				$params['content'] = $_POST['group_name'];
			}
			if(U('/Admin/User/editGroup') == $params['action']) {
				//编辑角色
				$params['content'] = $_POST['group_name'];
			}
			if(U('/Admin/User/deleteGroup') == $params['action']) {
				//删除角色
				$params['content'] = $_GET['group_name'];
			}
			if(U('/Admin/User/baseinfo') == $params['action']) {
				//修改资料
				$params['content'] = $_POST['user_name'];
			}
			if(U('/Admin/User/password') == $params['action']) {
				//修改密码
				$params['content'] = '';
			}
			if(U('/Admin/Cinema/add') == $params['action']) {
				//添加影院
				$params['content'] = '主机名：'.$_POST['host_name'];
			}
			if(U('/Admin/Cinema/edit') == $params['action']) {
				//编辑影院
			}
			if(U('/Admin/Cinema/update') == $params['action']) {
				//停用影院
				$status = I('request.status');
				if(1 == $status) {
					$params['is_show'] = 0;
				}
				if(2 == $status) {
					$params['is_show'] = 1;
				}
			}
			if(U('/Admin/Cinema/initPassword') == $params['action']) {
				//初始化密码
			}
		}
		!$params['content'] && $params['content'] = ''; 
		$db_result = $model->data($params)->add();
	}
	
	/**
	 * 查找指定日志
	 * @param  array
	 * @return array
	 * @author ming.nie
	 */
	public function find($id)
	{
		$log_options = getLogOptions();
		
		$model = M();
		$sql = 'SELECT a.*,b.user_name,b.nick_name 
				FROM mt_user_log AS a
				LEFT JOIN mt_user AS b ON a.create_user_id = b.user_id
				WHERE a.log_id = "'.$id.'"';
		$result = $model->query($sql);
		$result = $result[0];
		
		return $result;
	}
}
